package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Override
    public PageResponse selectByPage(Integer pageNum, Integer pageSize, String name, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProject> page = nursingProjectMapper.selectPageByNameAndStatus(name, status);
        return PageResponse.of(page, NursingProject.class);
    }

    @Override
    public void addNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.copyProperties(nursingProjectDto, NursingProject.class);
        nursingProject.setCreateBy(1671403256519078138L);
        //重名校验
        int num = nursingProjectMapper.countByName(nursingProject.getName());
        if (num > 0) {
            throw new BaseException(BasicEnum.DUPLICATE_NURSING_PROJECT);
        }
        nursingProjectMapper.insert(nursingProject);
    }

    @Override
    public NursingProjectVo selectById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.selectById(id);
        return BeanUtil.copyProperties(nursingProject, NursingProjectVo.class);
    }

    @Override
    public void update(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(nursingProjectDto, nursingProject);
        nursingProjectMapper.update(nursingProject);
    }

    @Override
    public void deleteById(Long id) {
        int count = nursingProjectPlanMapper.countByNursingProjectId(id);

        if (count > 0) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_DELETE_FAIL_HAS_PLAN);
        }

        nursingProjectMapper.deleteById(id);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        nursingProjectMapper.updateStatus(id, status);
    }

    @Override
    public List<NursingProjectVo> queryList() {
        return nursingProjectMapper.selectNursingProjectAll();
    }

}
